<template>
	<view class="home">
		<uni-search-bar @confirm="searchGoods" v-model="searchKey" />
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
			<swiper-item v-for="(item,index) in bannerGoodsList" :key="index">
				<view class="swiper-item">
					<image :src="item" mode="aspectFill"></image>
				</view>
			</swiper-item>
		</swiper>
		<!-- 商品分类 -->
		<view class="cate">
			<view class="cate-item" v-for="(item,index) in cateOneLst" :key="index">
				<image class="cate-img" :src="item.icon" mode="widthFix"></image>
				<view class="cate-name">{{item.name}}</view>
			</view>
		</view>

		<!-- 精选商品列表 -->
		<view class="showGoods">
			<view class="title">
				精选商品
			</view>
			<view class="list">
				<!-- <GoodsItem :selectedLst="selectedLst" :itemWidth="360" :maxSize="20"></GoodsItem> -->
				<view class="item" v-for="item,index in selectedLst" :key="index" @tap="toDetails(item.id)">
					<view class="img">
						<image :src="item.goods_big_logo" mode="widthFix"></image>
					</view>
					<view class="content">
						<view class="name">{{item.name}}
						</view>
						<view class="info">
							<text>{{item.price}}</text>
							<text class="tag">精选</text>
						</view>
					</view>
				</view>

			</view>
		</view>
	</view>
</template>

<script setup>
	import GoodsItem from "../../components/GoodsItem.vue"
	import {
		ref
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	// 搜索功能
	const searchKey = ref("")

	function searchGoods() {
		console.log(searchKey.value)
	}



	onLoad(() => {
		getBanner()
		getCateOneLst()
		getSelectedList()
	})


	// 轮播图
	const bannerGoodsList = ref([])
	// 获取轮播图图片
	function getBanner() {
		uni.$post("/goodsPromoteSelect", {
			"is_selected": 0,
			"is_promote": 1,
			"page": 1,
			"number": 5
		}).then((res) => {
			// console.log(res)
			if (res.code == 200) {
				// 将商品列表中的图片信息提取
				bannerGoodsList.value = res.goodsList.map(item => uni.$base_url + item.goods_big_logo)
				// 控制台显示测试接口数据
				// console.log(bannerGoodsList.value)
			} else {
				uni.showToast({
					icon: "error",
					title: '服务器错误，请稍后重试'
				})
			}
			console.log(bannerGoodsList)
		}).catch((err) => {
			uni.showToast({
				icon: "error",
				title: '网络请求错误'
			})
		})
	}

	//一级分类列表
	const cateOneLst = ref([])

	function getCateOneLst() {
		uni.$post("/categoryListLevel", {
			"level": 0
		}).then((res => {

			if (res.code == 200) {
				cateOneLst.value = res.result.map(item => {
					item.icon = uni.$base_url + item.icon
					return item
				})
				// console.log(cateOneLst.value)
			} else {
				uni.showToast({
					icon: "exception",
					title: "服务器错误"
				})
			}

		})).catch((err) => {
			uni.showToast({
				icon: "exception",
				title: "网络错误"
			})
		})
	}

	// 精选商品列表
	const selectedLst = ref([])

	function getSelectedList() {
		uni.$post("/goodsPromoteSelect", {
			"is_selected": 1,
			"is_promote": 0,
			"page": 1,
			"number": 30
		}).then(res => {
			// console.log(res)
			if (res.code == 200) {
				selectedLst.value = res.goodsList.map(item => {
					item.goods_big_logo = uni.$base_url + item.goods_big_logo
					return item;
				})
				// console.log(selectedLst.value);
			} else {
				uni.showToast({
					icon: "error",
					title: '服务器错误，请稍后重试'
				})
			}
		}).catch((err) => {

			uni.showToast({
				icon: "error",
				title: '网络请求错误'
			})
		})

	}
	
	const toDetails=(id)=>{
		uni.navigateTo({
			url:"/pages/goodsDetails/goodsDetails?id="+id
		})
	}
	
</script>

<style scoped>
	.home {
		background-color: #eee;
	}

	/* 轮播图中图片宽度 */
	.home .swiper-item>image {
		width: 100%;
	}

	/* 轮播图容器高度 */
	.home swiper {
		height: 200px;
	}

	/* 商品分类容器 */
	.home>.cate {
		display: flex;
		justify-content: space-around;
		background-color: #fff;
		margin: 10rpx 0 10rpx 0;
	}

	/* 每个商品分类 */
	.home>.cate>.cate-item {
		width: 130rpx;
		height: 130rpx;
		/* border:1px solid red; */
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size: 20rpx;

	}




	/* 商品分类中图片 */
	.home>.cate>.cate-item>.cate-img {
		width: 60rpx;
		height: 60rpx;
		margin-top: 20rpx;
	}

	/* 精选商品标题 */
	.showGoods>.title {
		background-color: #fff;
		padding: 15rpx;
		color: #ff6700;
	}

	.showGoods>.list {
		display: flex;
		justify-content: space-evenly;
		flex-wrap: wrap;
	}

	.showGoods>.list>.item {
		border: 1px solid #ddd;
		background-color: #fff;
		padding: 15rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		align-items: center;
		border-radius: 10rpx;
		box-shadow: 3px 3px 3px #ccc;
		margin-top: 10rpx;
		width: 350rpx;

	}

	.showGoods>.list>.item>.img {
		width: 95%;
		border: 1px solid #ddd;
	}

	.showGoods>.list>.item>.img>image {
		width: 100%;
		height: 100%;
	}

	.showGoods>.list>.item>.content {
		width: 95%;
		margin-top: 5rpx;
	}

	.showGoods>.list>.item>.content>.name {
		color: #666;
		font-size: 25rpx;
		margin: 8rpx 0;
		/* 下面的样式设置显示两行超出用省略号表示 */
		overflow: hidden;
		-webkit-line-clamp: 2;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;

	}

	.showGoods>.list>.item>.content>.info {
		display: flex;
		justify-content: space-between;
	}

	.showGoods>.list>.item>.content>.info>text:nth-child(1) {
		color: #f33;
		font-size: 30rpx;

	}

	.showGoods>.list>.item>.content .tag {
		background-color: #ff6700;
		padding: 5rpx 8rpx;
		color: #fff;
		font-size: 25rpx;
		border-radius: 6rpx;
	}
</style>